package com.example.botechotext;

import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import org.jolokia.client.J4pClient;
import org.jolokia.client.request.J4pReadRequest;
import org.jolokia.client.request.J4pReadResponse;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class JolokiaDemo {

    public static void main(String[] args) throws Exception {
        J4pClient j4pClient = J4pClient.url("http://172.25.65.37:8084/jolokia")
                .user("jolokia")
                .password("jolokia")
                .build();
        J4pReadRequest req = new J4pReadRequest("java.lang:type=Memory", "HeapMemoryUsage");
        J4pReadResponse resp = j4pClient.execute(req);
        Map<String, Object> vals = resp.getValue();
        long used = (int) vals.get("used");
        long max =(long) vals.get("max");
        int usage = (int) (used * 100 / max);
        System.out.println("Memory usage: used: " + used + " / max: " + max + " = " + usage + "%");
    }
    @Test
    public void test() throws Exception {
        J4pClient j4pClient = J4pClient.url("http://172.25.65.37:8084/jolokia")
                .user("jolokia")
                .password("jolokia")
                .build();
        J4pReadRequest req = new J4pReadRequest("java.lang:type=Memory", "NonHeapMemoryUsage");
        J4pReadResponse resp = j4pClient.execute(req);
        Map<String, Object>vals = resp.getValue();
        System.out.println(JSONObject.toJSONString(vals));
    }

    @Test
    public void test2() throws Exception {
        J4pClient j4pClient = J4pClient.url("http://172.25.65.37:8084/jolokia")
                .user("jolokia")
                .password("jolokia")
                .build();
        J4pReadRequest req = new J4pReadRequest("java.lang:type=OperatingSystem", "TotalMemorySize");
        J4pReadResponse resp = j4pClient.execute(req);
//        Map<String, Object>vals = resp.getValue();
        System.out.println(JSONObject.toJSONString(resp.getValue()));
    }

}
